package org.martin.snsrs.client.services.http;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;

public class HttpClientUtils {
	private static final int DEFAULT_MAX_CONNECTIONS = 10;
	private static final int DEFAULT_MAX_RETRIES = 5;
	private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
	private static final int DEFAULT_SOCKET_TIMEOUT = 10000;
	private static final String ENCODING_GZIP = "gzip";
	private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
	private static int maxConnections = 10;
	private static int socketTimeout = 10000;

	public static HttpClient getHttpClient() {
		BasicHttpParams httpParams = new BasicHttpParams();
		ConnManagerParams.setTimeout(httpParams, socketTimeout);
		ConnManagerParams.setMaxConnectionsPerRoute(httpParams,
				new ConnPerRouteBean(maxConnections));
		ConnManagerParams.setMaxTotalConnections(httpParams, 10);
		HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
		HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout);
		HttpConnectionParams.setTcpNoDelay(httpParams, true);
		HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
		HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

		SchemeRegistry localSchemeRegistry = new SchemeRegistry();
		localSchemeRegistry.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		DefaultHttpClient httpClient = new DefaultHttpClient(
				new ThreadSafeClientConnManager(httpParams, localSchemeRegistry),
				httpParams);

		httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
			public void process(HttpRequest paramHttpRequest,
					HttpContext paramHttpContext) {
				if (!paramHttpRequest.containsHeader("Accept-Encoding"))
					paramHttpRequest.addHeader("Accept-Encoding", "gzip");
			}
		});

		httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
			public void process(HttpResponse paramHttpResponse,
					HttpContext paramHttpContext) {
				Header localHeader = paramHttpResponse.getEntity()
						.getContentEncoding();
				if (localHeader != null) {
					HeaderElement[] arrayOfHeaderElement = localHeader
							.getElements();
					int i = arrayOfHeaderElement.length;
					for (int j = 0;; j++) {
						if (j < i) {
							if (!arrayOfHeaderElement[j].getName()
									.equalsIgnoreCase("gzip"))
								continue;
							paramHttpResponse.setEntity(new InflatingEntity(
									paramHttpResponse.getEntity()));
						}
						else
							return;
					}
				}
			}
		});

		return httpClient;
	}

	public static void setSSLSocketFactory(HttpClient paramHttpClient,
			SSLSocketFactory paramSSLSocketFactory) {
		paramHttpClient.getConnectionManager().getSchemeRegistry()
				.register(new Scheme("https", paramSSLSocketFactory, 443));
	}

	public static void setTimeout(HttpClient paramHttpClient, int paramInt) {
		HttpParams localHttpParams = paramHttpClient.getParams();
		ConnManagerParams.setTimeout(localHttpParams, paramInt);
		HttpConnectionParams.setSoTimeout(localHttpParams, paramInt);
		HttpConnectionParams.setConnectionTimeout(localHttpParams, paramInt);
	}

	public static void setUserAgent(HttpClient paramHttpClient,
			String paramString) {
		HttpProtocolParams.setUserAgent(paramHttpClient.getParams(),
				paramString);
	}
}